﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ConnectToDatabase;

namespace QLGiayDep.Forms
{
    /// <summary>
    /// Interaction logic for WinLogin.xaml
    /// </summary>
    public partial class WinLogin : Window
    {
        //UpdateDB m_updateDB;
        //DeleteDB m_deleteDB;
        private string url = "";
        string m_sMaDangNhap;

        public WinLogin()
        {
            InitializeComponent();
        }

        private bool KiemTraNhap()
        {
            if (txtUsername.Text == "" || pwbPassword.Password == "")
                return true;
            else return false;
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private bool KiemTraMatKhau()
        {
            SelectDB selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), url + "SELECTDB_OBJECT");
            string admin = "admin";
            if (pwbPassword.Password.Equals(admin) && txtUsername.Text.Equals(admin))
                return true;
            else
                if (selectDB.SelectMaLoaiNV_MaNV(txtUsername.Text) == null)
                    return false;
                else
                    if (txtUsername.Text == pwbPassword.Password)
                        return true;
                    else 
                        return false;
        }

        private void btnDangNhap_Click(object sender, RoutedEventArgs e)
        {
            if (KiemTraNhap())
                MessageBox.Show("Phải nhập đầy đủ thông tin trước khi đăng nhập", "Thông báo");
            else
            {
                if (KiemTraMatKhau())
                {
                    MainWindow m_fMainWindow = new MainWindow(txtUsername.Text, url);
                    m_fMainWindow.Show();
                    this.Hide();
                }
                else MessageBox.Show("Sai thông tin! Không đăng nhập được!", "Thông Báo");
            }
        }

        private void txtUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnDangNhap_Click(sender, e);
        }

        private void pwbPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnDangNhap_Click(sender, e);
        }

        private void btnKetNoi_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                TcpChannel tcpChannel = new TcpChannel();
                ChannelServices.RegisterChannel(tcpChannel, false);

                url = "tcp://" + txtIP.Text + ":" + txtPort.Text + "/";

                //tao the hien cua INSERTDB_OBJECT
                RemotingConfiguration.RegisterWellKnownClientType(typeof(InsertDB), url + "INSERTDB_OBJECT");

                //tao the hien cua SELECTDB_OBJECT
                RemotingConfiguration.RegisterWellKnownClientType(typeof(SelectDB), url + "SELECTDB_OBJECT");

                //tao the hien cua UPDATEDB_OBJECT
                RemotingConfiguration.RegisterWellKnownClientType(typeof(UpdateDB), url + "UPDATEDB_OBJECT");

                //tao the hien cua DELETEDB_OBJECT
                RemotingConfiguration.RegisterWellKnownClientType(typeof(DeleteDB), url + "DELETEDB_OBJECT");

                //tao the hien cua SHOWDB_OBJECT
                RemotingConfiguration.RegisterWellKnownClientType(typeof(ShowDB), url + "SHOWDB_OBJECT");

                //tao the hien cua OTHERHANDLE_OBJECT
                //RemotingConfiguration.RegisterWellKnownClientType(typeof(OtherHandle), url + "OTHERHANDLE_OBJECT");

                //tao the hien cua SETID_OBJECT
                RemotingConfiguration.RegisterWellKnownClientType(typeof(SetID), url + "SETID_OBJECT");

                //tao the hien cua EXPORTEXCEL_OBJECT
                RemotingConfiguration.RegisterWellKnownClientType(typeof(ExportExcel), url + "EXPORTEXCEL_OBJECT");

                //tao the hien cua SELECTDATA_OBJECT
                //RemotingConfiguration.RegisterWellKnownClientType(typeof(SelectData), url + "SELECTDATA_OBJECT");

                //InsertDB m_testInsert = new InsertDB();
                //SelectDB m_testSelect = new SelectDB();
                //UpdateDB m_testUpdate = new UpdateDB();
                //DeleteDB m_testDelete = new DeleteDB();
                //ShowDB m_testShow = new ShowDB();
                //OtherHandle m_testOther = new OtherHandle();
                //SetID m_testSetID = new SetID();
                //ExportExcel m_testExcel = new ExportExcel();
                //SelectData m_s = new SelectData();
                //m_testInsert = new InsertDB();
                //m_testSetID = new SetID();

                InsertDB insertDB = (InsertDB)Activator.GetObject(typeof(InsertDB), url + "INSERTDB_OBJECT");
                SelectDB selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), url + "SELECTDB_OBJECT");
                //SelectDB selectDB = new SelectDB();

                if (selectDB.SelectAllDSLoaiNV() == null)
                    MessageBox.Show("Kết nối thất bại!\nKiểm tra lại IP hoặc Port", "Thông Báo");
                else
                {
                    gridDangNhap.IsEnabled = true;
                    btnDangNhap.IsEnabled = true;
                }
                //m_s.SelectAllLoaiSanPham();
                ExportExcel m_testExcel = new ExportExcel();
            }
            catch
            {
                MessageBox.Show("Kết nối thất bại!\nKiểm tra lại IP hoặc Port", "Thông Báo");
            }            
        }
    }
}
